package com.shunda.pay.controller;

import com.shunda.common.core.controller.BaseController;
import com.shunda.common.core.domain.AjaxResult;
import com.shunda.pay.dto.WxChatBasePayDto;
import com.shunda.pay.dto.WxChatProfitSharingDto;
import com.shunda.pay.dto.WxChatQueryProfitsSharingDto;
import com.shunda.pay.dto.WxChatReceiversAddDto;
import com.shunda.pay.dto.WxTransferDto;
import com.shunda.pay.service.IWeChatPayService;
import com.shunda.trade.entity.TradeProfitSharingAccount;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 微信支付相关的接口
 */
@RestController
@RequestMapping("/wechat/pay")
public class WeChatPayController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(WeChatPayController.class);

    @Autowired
    private IWeChatPayService weChatPayService;

    /**
     * 支付
     */
    @PostMapping("/pay")
    public AjaxResult pay(@RequestBody WxChatBasePayDto param) {
        return success(weChatPayService.pay(param));
    }

    /**
     * 新增分账接收方
     *
     * @param param.deptId  加油站ID
     * @param param.type    枚举值：MERCHANT_ID：商户ID PERSONAL_OPENID：个人openid（由父商户APPID转换得到）
     * @param param.account 类型是MERCHANT_ID时，是商户号 类型是PERSONAL_OPENID时，是个人openid
     * @return
     */
    @PostMapping("/receivers/add")
    public AjaxResult receiversAdd(@RequestBody WxChatReceiversAddDto param) {
        TradeProfitSharingAccount receiversAdd = weChatPayService.receiversAdd(param, getUsername());
        return success(receiversAdd);
    }

    /**
     * 删除分账接收方
     *
     * @param param.deptId  加油站ID
     * @param param.type    枚举值：MERCHANT_ID：商户ID PERSONAL_OPENID：个人openid（由父商户APPID转换得到）
     * @param param.account 类型是MERCHANT_ID时，是商户号 类型是PERSONAL_OPENID时，是个人openid
     * @return
     */
    @PostMapping("/receivers/del")
    public AjaxResult receiversDel(@RequestBody WxChatReceiversAddDto param) {
        Boolean receiversDel = weChatPayService.receiversDel(param, getUsername());
        return success(receiversDel);
    }

    /**
     * 手动分账
     *
     * @param param.orderId   订单id
     * @param param.wxOrderId 微信订单id
     * @return
     */
    @PostMapping("/to/sharing")
    public AjaxResult createProfitSharingOrder(@RequestBody WxChatProfitSharingDto param) {
        String profitSharingOrder = weChatPayService.createProfitSharingOrder(param);
        return success(profitSharingOrder);
    }

    /**
     * 查询分账结果
     *
     * @param param.outOrderNo    订单id
     * @param param.transactionId 微信订单id
     * @return
     */
    @PostMapping("/query/Sharing")
    public AjaxResult queryProfitSharing(@RequestBody WxChatQueryProfitsSharingDto param) throws Exception {
        String profitSharingResult = weChatPayService.queryProfitSharing(param);
        return success(profitSharingResult);
    }

    /**
     * 转账到零钱
     *
     * @param param.deptId   油站ID
     * @param param.userId   用户ID
     * @param param.type     提现方（油站：1，用户：2）
     * @param param.openId   收款用户OpenID
     * @param param.userName 收款用户姓名
     * @param param.amount   转账金额（单位：分）
     * @return
     */
    @PostMapping("/transfer")
    public AjaxResult transferToWallet(@RequestBody WxTransferDto param) {
        String profitSharingResult = weChatPayService.transferToWallet(param);
        return success(profitSharingResult);
    }
}
